package com.doit.day02;

//创建一个topic 往里面不断的写数据   event-log
//创建一个消费者，去读取数据 ==》 写回到kafka里面去 另一个topic

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.ProducerFencedException;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;

//开启事务，开启幂等性
//都是生产者的属性
public class _05_事务的练习 {
    public static void main(String[] args) {

        //创建一个消费者  读取数据
        Properties props = new Properties();
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "linux01:9092");
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "end2");
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");//默认值是latest
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");


        //生产者的属性
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "linux01:9092");
        props.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        //开启幂等性
        props.setProperty(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true");
        //开启幂等性以后还需要有三个参数配置
        props.setProperty(ProducerConfig.ACKS_CONFIG, "all");
        props.setProperty(ProducerConfig.RETRIES_CONFIG, "2");
        props.setProperty(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, "4");
        //开启事务
        props.setProperty(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "doit40");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
        //创建一个生产者
        KafkaProducer<String, String> producer = new KafkaProducer<>(props);
        //初始化事务
        producer.initTransactions();

        consumer.subscribe(Arrays.asList("event-log"));
        //poll  for循环读取到对应的数据
        while (true) {
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Integer.MAX_VALUE));
            try {
                //开启事务
                producer.beginTransaction();
                for (ConsumerRecord<String, String> record : poll) {
                    //读取的数据，也是我需要发送出去的数据
                    String value = record.value();
                    System.out.println(value);
                    ProducerRecord<String, String> pr = new ProducerRecord<>("end", value);
                    //producer发送数据
                    producer.send(pr);
                    producer.flush();
                    //半自动提交偏移量
                    consumer.commitAsync();
                }
                //提交事务
                producer.commitTransaction();
            } catch (ProducerFencedException e) {
                //回滚事务，放弃事务
                producer.abortTransaction();
            }
        }


    }
}
